//
//  NRWFilterView.m
//  NRWMall
//
//  Created by 邢毅 on 2018/6/7.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWBuyCarSortView.h"
#import "NRWSingleChooseCell.h"
#import "NRWBuyCarFiltermanager.h"
#import "NRWBuyCarConditionModel.h"

@interface NRWBuyCarSortView()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *mTableView;

@property (copy, nonatomic) NSString *selectSortName;

@property (strong, nonatomic) NSArray<NRWBuyCarSortModel *> *sortContentModelArr;

@end

@implementation NRWBuyCarSortView
{
    CGFloat cellHeight;
}

- (void)setSortContentModelArr:(NSArray<NRWBuyCarSortModel *> *)sortContentModelArr {
    
    self.sortContentModelArr = sortContentModelArr;
    
    [self setContentViewHeight:cellHeight * sortContentModelArr.count];
    
    [self.mTableView reloadData];
}

- (void)setSortContentModelArr:(NSArray<NRWBuyCarSortModel *> *)sortContentModelArr selectSortName:(NSString *)selectSortName {
    
    _sortContentModelArr = sortContentModelArr;
    
    if (isEmptyString(selectSortName)) {
        self.selectSortName = @"默认排序";
    } else {
        self.selectSortName = selectSortName;
    }
    
    [self setContentViewHeight:cellHeight * sortContentModelArr.count];
    
    [self.mTableView reloadData];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        cellHeight = 50;
        
        [self setupUI];
        
        [self prepare];
    }
    return self;
}

- (void)setupUI {
    
    _mTableView = [[UITableView alloc] init];
    _mTableView.delegate = self;
    _mTableView.dataSource = self;
    _mTableView.backgroundColor = [UIColor whiteColor];
    [_mTableView registerClass:NRWSingleChooseCell.class forCellReuseIdentifier:@"cell"];
    [self addSubview:_mTableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.sortContentModelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NRWSingleChooseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.title = self.sortContentModelArr[indexPath.row].sortName;
    cell.didSelect = [self.sortContentModelArr[indexPath.row].sortName isEqualToString:self.selectSortName];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return cellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NRWBuyCarSortModel *model = self.sortContentModelArr[indexPath.row];
    
    if ([self.delegate respondsToSelector:@selector(buyCarSortView:sortTitle:sortId:)]) {
        
        [self.delegate buyCarSortView:self sortTitle:model.sortName sortId:model.sortId];
    }
}

#pragma mark - overrideSuperMethod
- (UIView *)bulletContentView {
    
    return self.mTableView;
}

- (BulletContentViewAlignment)contentViewAlignment {
    
    return BulletAlignmentTop;
}

- (BOOL)touchBackgroundCanClose {
    
    return YES;
}

@end
